﻿@{ 
    Layout = "_AdminLayout";
    ViewData["Title"] = "系统概况";
}
@section pagestyle{
    <!-- DataTables -->
    <link rel="stylesheet" href="/plugins/datatables-bs4/css/dataTables.bootstrap4.css">
    <link rel="stylesheet" href="/rate/iconfont.css">

    <!--Select2-->
    <link rel="stylesheet" href="~/plugins/select2/css/select2.min.css">
    <link rel="stylesheet" href="~/plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css">
    <style type="text/css">
        .pagination li * {
            outline: none !important;
            outline-style: none !important
        }

        .userinfo {
            display: flex;
            align-items: center;
        }

            .userinfo span {
                padding: 5px;
            }

                .userinfo span img {
                    width: 50px;
                }

        #mdChangeWorker .select2-container .select2-selection--single, #mdChangeWorker .select2-selection__arrow {
            height: 70px;
            line-height: 70px;
        }
    </style>
}

<!-- Content Header (Page header) -->
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0 text-dark">数据统计</h1>
            </div>
        </div><!-- /.row -->
    </div><!-- /.container-fluid -->
</div>

<!-- /.content-header -->
<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <!-- Info boxes -->
        <div class="row">
            <div class="col-12 col-sm-6 col-md-3">
                <div class="info-box">
                    <span class="info-box-icon bg-info elevation-1"><i class="fas fa-user"></i></span>
                    <div class="info-box-content">
                        <span class="info-box-text">普通用户</span>
                        <span class="info-box-number">
                            @ViewBag.UserNum
                            <small>人</small>
                        </span>
                    </div>
                    <!-- /.info-box-content -->
                </div>
                <!-- /.info-box -->
            </div>
            <!-- /.col -->
            <div class="col-12 col-sm-6 col-md-3">
                <div class="info-box mb-3">
                    <span class="info-box-icon bg-warning elevation-1"><i class="fas fa-user-tie"></i></span>
                    <div class="info-box-content">
                        <span class="info-box-text">工程师</span>
                        <span class="info-box-number">
                            @ViewBag.WorkerNum
                            <small>人</small>
                        </span>
                    </div>
                    <!-- /.info-box-content -->
                </div>
                <!-- /.info-box -->
            </div>
            <!-- /.col -->
            <!-- fix for small devices only -->
            <div class="clearfix hidden-md-up"></div>
            <div class="col-12 col-sm-6 col-md-3">
                <div class="info-box mb-3">
                    <span class="info-box-icon bg-blue elevation-1"><i class="fas fa-tasks"></i></span>

                    <div class="info-box-content">
                        <span class="info-box-text">维修工单</span>
                        <span class="info-box-number">
                            @ViewBag.WorkOrderNum
                            <small>个</small>
                        </span>
                    </div>
                    <!-- /.info-box-content -->
                </div>
                <!-- /.info-box -->
            </div>
            <!-- /.col -->
            <div class="col-12 col-sm-6 col-md-3">
                <div class="info-box mb-3">
                    <span class="info-box-icon bg-success elevation-1"><i class="fas fa-donate"></i></span>
                    <div class="info-box-content">
                        <span class="info-box-text">支付订单</span>
                        <span class="info-box-number">
                            @ViewBag.PaymentNum
                            <small>个</small>
                        </span>
                    </div>
                    <!-- /.info-box-content -->
                </div>
                <!-- /.info-box -->
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->

        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title">注册机构</h5>
                        <div class="card-tools">
                            <button type="button" class="btn btn-tool" data-card-widget="collapse">
                                <i class="fas fa-minus"></i>
                            </button>
                        </div>
                    </div>
                    <!-- /.card-header -->
                    <div class="card-body">
                        <div class="row">
                            <div class="col-12">
                                <div class="card">
                                    <!-- /.card-header -->
                                    <div class="card-body">
                                        <table id="tbOrganizations" class="table table-bordered table-striped" width="100%">
                                            <thead>
                                                <tr>
                                                    <th width="10">#</th>
                                                    <th width="200">机构名称</th>
                                                    <th>营业区域</th>
                                                    <th>用户昵称</th>
                                                    <th>创建时间</th>
                                                    <th>状态</th>
                                                    <th>操作</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                            </tbody>
                                        </table>
                                    </div>
                                    <!-- /.card-body -->
                                </div>
                                <!-- /.card -->
                            </div>
                        </div>
                        <!-- /.row -->
                    </div>
                    <!-- ./card-body -->
                </div>
                <!-- /.col -->
            </div>
        </div>
        <!-- /.row -->
    </div><!--/. container-fluid -->
</section>
<!-- /.content -->

<div class="modal fade" id="mdChangeWorker" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" style="top:calc(50% - 300px)">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">更换工程师</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-sm-6">
                        <!-- text input -->
                        <div class="form-group">
                            <label>现在工程师</label>
                            <div class="userinfo">
                                <span>
                                    <img src="" id="avatar" />
                                </span>
                                <span id="name">

                                </span>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label>更换为</label>
                            <select id="SelectWorder" class="form-control" data-placeholder="请选择工程师" style="width: 80%;">
                            </select>
                        </div>
                    </div>
                </div>
                <input type="hidden" id="orgId" />
            </div>
            <div class="modal-footer justify-content-between">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button id="btnSubmitChange" type="button" class="btn btn-primary">提交</button>
            </div>
        </div>
    </div>
</div>

@section pagescript{

    <!-- DataTables -->
    <script src="~/plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="~/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
    <script src="~/js/custom/datatablehelper.js"></script>

    <!-- Select2 -->
    <script src="~/plugins/select2/js/select2.full.min.js"></script>
    <script src="~/plugins/select2/js/i18n/zh-CN.js"></script>

    <script>
        jQuery(function ($) {
            colModel = [
                { "data": null, "targets": 0 },
                { "data": "orgName" },
                { "data": "businessArea" },
                {
                    "data": "nickName", "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        var content = "";
                        content = `<div class="userinfo"><span><img src ="${oData.avatar}"/></span>${oData.nickName}<span></span></div>`;
                        $(nTd).html(content);
                    }
                },
                {
                    "data": "submitTime",
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        $(nTd).html($.tools.formatJsonDate(oData.submitTime, "yyyy-MM-dd hh:mm:ss"));
                    }
                },
                {
                    "data": "auditStatus",
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        var status = oData.auditStatus;
                        var content = "";
                        if (status == 2) {
                            content = "<span style='color:#28a745'>已审核<span>";
                        } else if (status == 1) {
                            content = "<span style='color:#dc3545'>待审核<span>";
                        } else if (status == 3) {
                            content = "<span style='color:#dc3545'>未通过<span>";
                        }
                        $(nTd).html(content);
                    }
                },
                {
                    "data": null,
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        var content = "";
                        var status = oData.auditStatus;
                        if (status == 1) {
                            content = `<a href="/organization/audit/${oData.id}">审核</a>`;
                        }
                        if (status == 2) {
                            content = `<a href="javascript:ChangeWorker('${oData.id}','${oData.workerID}')">更换工程师</a>`;
                        }
                        $(nTd).html(content);
                    }
                }
            ];
            $(dataTableInit("#tbOrganizations", "/organization/getorganizations", colModel, ''));
        });

        function ChangeWorker(orgId, woekerId) {
            var actionUrl = `/user/getworkers`;
            var query = { PageIndex: 1, PageSize: 99999, Keywords: "", Where: {} }
            AjaxPost(actionUrl, JSON.stringify(query), function (result) {
                if (result.code == 200) {
                    var workers = [];
                    $.each(result.data, function (i, worker) {
                        if (worker.id == woekerId) {
                            $("#name", "#mdChangeWorker").html(worker.nickName);
                            $("#avatar", "#mdChangeWorker").attr("src", worker.avatar);
                        }
                        else {
                            workers.push({ id: worker.id, text: worker.nickName, avatar: worker.avatar });
                        }
                    });

                    var instance = $("#SelectWorder").data("select2");
                    if (instance) {
                        $("#SelectWorder").select2("destroy").empty();
                    }

                    $("#SelectWorder").select2({
                        language: "zh-CN",
                        tags: workers,
                        templateSelection: formatState,
                        templateResult: formatState,
                        minimumResultsForSearch: Infinity
                    });

                    $("#orgId", "#mdChangeWorker").val(orgId);

                    $("#mdChangeWorker").modal();
                }
            });
        }

        function formatState(state) {
            if (!state.id) {
                return state.text;
            }
            if (state.text != null && state.text != "") {
                var $state = $(
                    `<div class="userinfo"><span><img src="${state.avatar}" /></span><span>${state.text}</span></div>`
                );
                return $state;
            }
        };

        $(function () {
            $("#btnSubmitChange").on("click", function () {
                var actionUrl = "/organization/changeworker";
                var ids = [];
                ids.push($("#orgId", "#mdChangeWorker").val());
                ids.push($("#SelectWorder").val());
                AjaxPost(actionUrl, JSON.stringify(ids), function (result) {
                    if (result.code == 200) {
                        $('#tbUsers').DataTable().ajax.reload(null, false);
                        alert("操作成功！");
                        $("#mdChangeWorker").modal("hide");
                    }
                    else {
                        alert(result.msg, "error");
                    }
                });
            });
        });
    </script>
}